const qiniuUploader = require("../../utils/qiniuUploader");

var appInstance = getApp();
var pageData    = {
  data: {
    add1:[true],
    postIm1:[],
    disabled:[false,false,false],
    input_value:{valueNull:''},
    change_com:{
      BizCatlg:'',
      BizCatlgId:'',
      subCatlg:'',
      subCatlgId:''
    }
  },
  app_title: '石雕小程序',
  app_description: '加油!',
  page_router: 'page10010',
  page_router1: '10010',
  onLoad: function (e) {
    var that =this;
    if(e.companyId!='add'){
      var param = appInstance.globalData.session_key;
      var companyDetail = {
          source : '1',
          userId : param.userId,
          companyId : e.companyId,
          token:param.myXcxSessionId
        }
        appInstance.sendRequest({
          url:'/company/companyDetail',
          data:companyDetail,
          method:'POST', 
          success: function(res){
            console.log(res.msg)
            if(res.result == 0){ 
              var add1 = [true],postIm1=[];
              if(res.companyLogo){
                add1 = [false,res.companyLogo]
                postIm1 = [res.companyLogo]
                 }                               
            var input_value={
                  companyName:res.companyName,
                  companyId:e.companyId,
                  vendorCatlg:res.vendorCatlg,
                  companySite:res.companySite,
                  contactPerson:res.contactPerson,
                  contactTel:res.contactTel,
                  companyBrief:res.companyBrief,
                  valueNull:''
                  }    
              that.setData({
                add1:add1,
                postIm1:postIm1,
                disabled:[false,false,false],
                input_value:input_value,
                change_com:{
                  bizCatlg:res.bizCatlg,
                  bizCatlgId:res.bizCatlgId||'',
                  subCatlg:res.subCatlg||'',
                  subCatlgId:res.subCatlg||''
              }
            })          
            }
          },
          fail: function(res){
            console.log('companydetail fail');
          }         
        })
       }         
  },
  onShow: function(){
    var change_com = appInstance.globalData.change_com;
      if(change_com!=''){
        this.setData({
          change_com:change_com
        })
        appInstance.globalData.change_com='';
      }
  },   
  didPressChooesImage: function(e) {
    console.log(e)
    var that = this;
    var tapeven = e.currentTarget.id;
    var param;
    wx.showModal({
      title: '提示',
      content: '确认上传？',
      success: function(res) {
        if (res.confirm) {
           initQiniu();
           if(tapeven=='postIm1'){
             param = that.data.add1.slice(1);
           }else if(tapeven=='postIm2'){
             param = that.data.add2;
           }else if(tapeven=='postVio'){
             param = that.data.add3.slice(1);
           }
           appInstance.showToast({title: '上传中...',icon: 'loading'});
           for (var i = 0; i < param.length; i++) {              
              qiniuUploader.upload(param[i], (res) => {
                 if(tapeven=='postIm1'){
                   var postIm1 = that.data.postIm1;
                        postIm1.push(res.imageURL);
                   var disabled = that.data.disabled;
                        disabled[0]=true;                 
                   that.setData({
                     postIm1: postIm1,disabled:disabled
                     });
                    }else if(tapeven=='postIm2'){
                      var postsrc = {img:res.imageURL}
                     var postIm2 = that.data.postIm2;
                        postIm2.push(postsrc);
                    var disabled = that.data.disabled;
                        disabled[1]=true;  
                   that.setData({
                     postIm2: postIm2,disabled:disabled});
                    }else if(tapeven=='postVio'){
                      var postsrc = {video:res.imageURL}
                    var postVio = that.data.postVio;postVio.push(postsrc);
                    var disabled = that.data.disabled;disabled[2]=true; 
                   that.setData({postVio: postVio,disabled:disabled});
                    }
                console.log(res); 
                appInstance.showToast({title: '上传成功',icon: 'success'});
                }, (error) => {
                  console.error('error: ' + JSON.stringify(error));    
                })
            }
        }
      }
    })  
  },
formSubmit:function(e){
    var that =this;
    var change_com= that.data.change_com;
    //var tapeven = e.currentTarget.id;
    var param = appInstance.globalData.session_key;
    var formparam = e.detail.value;
    console.log(e);
    //console.log(that.data.postIm1[0]);
    if(formparam.companyName!=''&that.data.postIm1[0]!=undefined){
    wx.showModal({
      title: '提示',
      content: '确认提交？',
      success: function(res) {
        if (res.confirm) {
             var postDetail = {
                companyName:formparam.companyName,
                companyId:that.data.input_value.companyId||'',
                bizCatlg:change_com.BizCatlgId,
               // subCatlg:change_com.subCatlgId,
                vendorCatlg:formparam.vendorCatlg,
                website:formparam.companySite,
                contactPerson:formparam.contactPerson,
                phone:formparam.contactTel,
                companyBrief:formparam.companyBrief,
                source : '1',
                userId : param.userId,
                token:param.myXcxSessionId,
                companyLogo:that.data.postIm1[0],
                defaultSign:''
              } 
          appInstance.sendRequest({
              url:'/company/modifyCompany',
              data:postDetail,
              method:'POST',               
              header:{
                'content-type':'application/json'
              },
              success: function(res){
                if(res.result == 0){ 
                  wx.navigateBack({
                    delta: 2, // 回退前 delta(默认为1) 页面
                    success: function(res){
                      // success
                    }
                  })                               
                  console.log(res.msg)
                }else{
                  console.log(res.msg)
                  //提示无数据并navigateback
                }
              },
              fail: function(res){
                console.log('getmode fail');
              },
              complete: function(res){
              }
           },'https://bc.zhaojx.net/bizcat/webservice/rest')   
         //  appInstance.showToast({title: '提交表单...',icon: 'loading'});
        }
      },
    }) 
  }else if(formparam.companyName == ''){
    appInstance.showToast({title: '请输入公司名称！',icon: 'loading'});
  }else if(that.data.change_com.BizCatlg == ''){
    appInstance.showToast({title: '请输入行业分类！',icon: 'loading'});
  }else if(that.data.postIm1[0]== undefined){
    appInstance.showToast({title: '请上传公司LOGO！',icon: 'loading'});
  }  
},
  tapDelete:function(e){
    var that = this;
    console.log(e);
    var tapeven = e.currentTarget.id;
    wx.showModal({
      title: '提示',
      content: '确认删除？',
      success: function(res) {
        if (res.confirm) {
          if(tapeven=='add'){
          var add = [true];
          var postIm1= [];
          that.setData({add1:add,postIm1:postIm1})
          }else if(tapeven=='dvideo'){
            var add3 = [true];
            var postVio= [];
            that.setData({add3:add3,postVio:postVio})
          }else{
          var add = that.data.add2;
          console.log(tapeven)
          add.splice(tapeven,1);
          var postIm2=that.data.postIm2;
          postIm2.splice(tapeven,1);
          that.setData({add2:add,postIm2,postIm2}) 
          }
        }
      }
    })
  },
  tapInnerLinkHandler:function(e){
    var that = this;
    var tapeven = e.currentTarget.id;
    console.log(e);
    if(tapeven =='add1'||tapeven =='add2'){
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album','camera'],
        success: function (res) { 
          console.log(res);       
          var tempFilePath = res.tempFilePaths[0];
          console.log(tempFilePath)
          if(tapeven=='add1'){
          var add = [false,tempFilePath];
            that.setData({add1:add})
            }else{
              var add =that.data.add2;add.push(tempFilePath);
              console.log(add)
              that.setData({add2:add})}
        },
        fail:function(res){
          console.log(res);
        },
        complete:function(res){
          console.log(res);
        }
      })
    }else if(tapeven =='videocamera'){
      wx.chooseVideo({
        sourceType: ['album','camera'],
        maxDuration: 60,
        camera: 'back',
        success: function(res) {
          var add3 = [false,res.tempFilePath];
          that.setData({add3: add3 })
        }
      })
    } else{
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['camera'],
        success: function (res) {        
          var tempFilePaths = res.tempFilePaths;
          if(tapeven=='add1_c'){
          var add = [false,tempFilePath];
            that.setData({add1:add})
            }else if(tapeven=='add2_c'){
              var add =that.data.add2;add.push(tempFilePath);
              that.setData({add2:add})}         
         //uploadfile
        }
      })
    }
 },
chooseCa:function(){
    wx.navigateTo({
        url: '../pagehyxz/pagehyxz?returnpage=addcompany',
        success: function(res){
          // success
        }      
     })
 }
    // var tab = JSON.parse(tapeven);
    // console.log(tab.inner_page_link); 
};
Page(pageData);
// 初始化七牛相关参数
function initQiniu() {
  var options = {
    region: 'ECN', // 华东区
    uptokenURL: 'https://bc.zhaojx.net/bizcat/webservice/rest/login/GetQnUpToekn',
    // uptoken: 'xxxx',
    domain: 'http://oh4xneden.bkt.clouddn.com/'
  };
  qiniuUploader.init(options);
}